package com.ctcd.security.service;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctcd.security.mapper.UserRepository;
import com.ctcd.security.pojo.User;
import com.ctcd.security.pojo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl {

    @Autowired()
    private UserRepository userRepository;

    public int changeDept(List<UserData> users){

        int totalUpdated=0; //处理的数量
        int batchNumber=100; //批处理数量
        int fromIndex = 0; // 当前处理的起始索引

        while(fromIndex<users.size()){
            int toIndex = Math.min(fromIndex + batchNumber, users.size()); // 计算当前处理的结束索引
            for (int i=fromIndex;i<toIndex;i++){
                UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
                userUpdateWrapper.eq("name", users.get(i).getName()); //判断
//                userUpdateWrapper.set("DEPT_CODE",users.get(i).getDeptNum()); //设置部门number
                int count=userRepository.update(null, userUpdateWrapper);
               totalUpdated=totalUpdated+ count;
            }
            fromIndex = toIndex; // 更新起始索引为下一个批次的起始位置

        }

 return totalUpdated;
    }

}
